/*
 * TANListDialog.java
 * 
 * Dialog in which the user enters the information that is required to create a new TAN list.
 * 
 * 
 * Copyright (C) 2008 Fraunhofer SCAI, Germany
 * All rights reserved
 * 
 * https://gforge.beingrid.eu/gf/project/scailm
 */

package de.fraunhofer.scai.lm.gui;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Dialog class showing the licence information.
 * 
 * @version $LastChangedRevision$
 * @author Bea Hornef
 * @author Yona Raekow
 */

public class TanListDialog extends Dialog {

    /**
     * Shell of the dialog;
     */
    private Shell myShell;

    /**
     * Cancel button.
     */
    private Button myCancelButton;

    /**
     * Ok button.
     */
    Button myOkButton;

    /**
     * The number of the new TAN list.
     */
    Integer myTanListNumber;

    /**
     * Number of TANs in the new TAN list.
     */
    Integer myNumberOfTANs;

    /**
     * Logger for the TanListDialog class.
     */
    static Logger logger = Logger
	    .getLogger(TanListDialog.class);
    /**
     * Constructor.
     * 
     * @param tanListNumber
     *                the number of the current TAN list.
     * @param parentShell
     *                parentShell the parent shell.
     */
    public TanListDialog(Integer tanListNumber, Shell parentShell) {
		super(parentShell);
		myTanListNumber = tanListNumber;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
		// create ok-button
		myOkButton = createButton(parent, IDialogConstants.OK_ID,
			IDialogConstants.OK_LABEL, true);
		myOkButton.setEnabled(false);
	
		// create cancel-button
		myCancelButton = createButton(parent, IDialogConstants.CANCEL_ID,
			IDialogConstants.CANCEL_LABEL, true);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Create TAN List");
		myShell = newShell;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {

		Composite composite = (Composite) super.createDialogArea(parent);
	
		GridLayout layout = new GridLayout(2, false);
		layout.marginTop = 10;
		layout.marginBottom = 10;
		layout.marginLeft = 10;
		layout.marginRight = 10;
		composite.setLayout(layout);
	
		// Initialisation of the GUI components of the dialog area.
		if (myTanListNumber.intValue() > 0) {
		    final Label commentLabel = new Label(composite, SWT.NONE);
		    commentLabel.setText("REMEMBER: The actual TAN list no "
			    + myTanListNumber.intValue() + " will be closed.");
		    // dummy label
		    new Label(composite, SWT.NONE);
		}
	
		final Label nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setText("Please enter a valid number of TANs for the list:"); //$NON-NLS-1$
	
		final Text tanText = new Text(composite, SWT.BORDER);
		tanText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		tanText.setText("");
	
		tanText.addListener(SWT.Modify, new Listener() {
		    public void handleEvent(Event event) {
				try {
				    myNumberOfTANs = new Integer(tanText.getText());
				    myOkButton.setEnabled(true);
				    myShell.setDefaultButton(myOkButton);
				} catch (Exception e) {
				    myOkButton.setEnabled(false);
				    myShell.setDefaultButton(myCancelButton);
				    logger.info("give an interger to create a number of TANs" + e.getMessage());
				    
				}
		    }
		});
	
		return composite;
    }

    /**
     * Returns the number of TANs in the TAN list.
     * 
     * @return the number of TANs
     */
    public Integer getNumberOfTANs() {
    	return myNumberOfTANs;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
     */
    protected void cancelPressed() {
    	myNumberOfTANs = null;
    	super.cancelPressed();
    	logger.debug("cancel button of TanListDialog pressed");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#close()
     */
    public boolean close() {
    	return super.close();
    }

}
